<?php
class PaymentShipping extends PaymentData {

	protected $_shippingAr;
	
	public function __construct(){
		$this->setName('shippingInfo');
		$this->_shippingAr = new CustomerOrderShippingInfo();
	}
	
	public function setShipping($customerId){
		$this->_shippingAr->customer_id = $customerId;
		$shippingInfo = $this->_shippingAr->find('t.customer_id = '.$this->_shippingAr->customer_id);
		if (!empty($shippingInfo)){
			$this->_shippingAr = $shippingInfo;
		}
		return $this;
	}
	
	public function setShippingInfo($shippingInfo){
		
		$this->_shippingAr->attributes = $shippingInfo;
		if(!empty($shippingInfo['shipping_ffl_dealer_name'])){
			$this->_shippingAr->shipping_ffl_dealer_name=$shippingInfo['shipping_ffl_dealer_name'];
		}
		return $this;
	}
	
	public function saveInfo(){	
		if(!empty($this->_shippingAr->id)){
			return CustomerOrderShippingInfo::model()->updateAll($this->_shippingAr->attributes,'customer_id ='.$this->_shippingAr->customer_id);
		}else{
			$shippingInfo = new CustomerOrderShippingInfo();
			foreach ($this->toArray() as $key=>$val){
				$shippingInfo->$key = $val;
			}
			$shippingInfo->customer_id = $this->_shippingAr->customer_id;
			$shippingInfo->shipping_ffl_dealer_name="a";
			$shippingInfo->save(true);
			return $shippingInfo->id;
		}
		
	}
	
	public function updateInfo(){
		$shippingInfo = $this->toArray();
		return CustomerOrderShippingInfo::model()->updateAll($shippingInfo,'customer_id ='.$this->_shippingAr->customer_id);
	}
	
	public function vaild(){
		if (empty($this->_shippingAr->customer_id)){
			$this->setErrorMessage('customerId','customerId NO');
		}
		return $this->vaild;
	}
	
	public function toArray(){
		$toArray = $this->_shippingAr->attributes;

		if(!empty($this->_shippingAr->shipping_ffl_dealer_name)){
			$toArray['shipping_ffl_dealer_name']=$this->_shippingAr->shipping_ffl_dealer_name;
		}
		return $toArray;
	}
	
	public function __get($value){
		return $this->_shippingAr->$value;
	}
	
	public function __set($key,$value){
		return $this->_shippingAr->$key = $value;
	}
}